#include <stdio.h>
#include "dclcd.h"
#include "tskcfg.h"
#include "bsp.h"
#include "parameter.h"
#include <stdlib.h>

static void mute(void);
static void power(void);
static void keyt(void);
static void set(void);
static void menu(void);
static void keyret(void);
static void enter(void);
static void down(void);
static void up(void);
static void left(void);
static void right(void);
static void ok(void);
static void volinc(void);
static void voldec(void);
static void key0(void);
static void key1(void);
static void key2(void);
static void key3(void);
static void key4(void);
static void key5(void);
static void key6(void);
static void key7(void);
static void key8(void);
static void key9(void);

static void load(uint8_t nowpage, uint8_t nowfocus);
static void update(uint8_t keyvalue);

static volatile uint8_t now_page = 0;
static volatile uint8_t now_focus = 0;
static const uint8_t ssid = 4;
static const Sto_MaintainStru *pmt = NULL;

const static GUI_FormKeysStru gui_parainput_keys = 
{
	.mute =   NULL,
	.power =  NULL,
  .keyt =   keyt,
  .set =    NULL,
  .home =   gui_form_common_home,
  .menu =   NULL,
  .keyret = keyret,
  .enter =  NULL,
	.down =   NULL,
	.up =     NULL,
	.left =   left,
	.right =  right,
	.ok =     ok,
	.volinc = NULL,
	.voldec = NULL,
	.key0 =   key0,
	.key1 =   key1,
	.key2 =   key2,
	.key3 =   key3,
	.key4 =   key4,
	.key5 =   key5,
	.key6 =   key6,
	.key7 =   key7,
	.key8 =   key8,
	.key9 =   key9,
};

const GUI_FormStru gui_from_parainput = 
{
	.load    = load,
	.update  = update,
	.keys    = &gui_parainput_keys,
  .refresh = &gui_maxdelay_refresh_time,
};

static void load(uint8_t nowpage, uint8_t nowfocus)
{
  uint8_t err;

  now_page = nowpage;
  now_focus = nowfocus;

  dc_cmd_change_screen(ssid);
  dc_comp_showhide(ssid, 13, 1);

  pmt = NULL;

  err = dc_display_parainput(now_page, now_focus, &sto_maintain_table, (const Sto_MaintainStru **)&pmt);
  if((err != 0)||(pmt == NULL))
  {
    gui_form_exec(&gui_from_paradisp, now_page, 0);
    
    return;
  }
  
  if(pmt->input != NULL)
  {
    pmt->input(pmt, STO_MTDINPUT_CMD_INIT, 0);
  }
  
  if(pmt->dynstr != NULL)
  {
    pmt->dynstr(pmt, STO_MTDYNSTR_OUTPUT_SRCVALUE, pmt->inputbuf, 12);
  }

  dc_cmd_fixcursor(1);
}

static void update(uint8_t keyvalue)
{
  dc_cmd_set_str(ssid, 10, pmt->inputbuf);
}

static void keyt(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEYT, 0);
}

static void set(void)
{
}

static void menu(void)
{
}

static void keyret(void)
{
  gui_form_exec(&gui_from_paradisp, now_page, 0);
}

static void enter(void)
{
}

static void down(void)
{
}

static void up(void)
{
}

static void left(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_DEL1, 0);
}

static void right(void)
{
  if((pmt->inputpos == NULL) || (pmt->inputbuf == NULL)){
    return;
  }

  (*(pmt->inputpos)) = 0;
  pmt->dynstr(pmt, STO_MTDYNSTR_OUTPUT_SRCVALUE, pmt->inputbuf, 12);
}

static void ok(void)
{
  uint8_t err;
  
  err = pmt->input(pmt, STO_MTDINPUT_CMD_KEYOK, 0);
  switch(err)
  {
    case 1:
      gui_form_exec(&gui_from_paradisp, now_page, 0);
      break;

    case 2:
      dc_display_parainput_err(1);
      return;

    case 3:
      dc_display_parainput_err(9);
      break;

    case 0:
      gui_form_exec(&gui_from_paradisp, now_page, 0);
      break;
  
    default:
      dc_display_parainput_err(err-5);
      return;
  }
}

static void volinc(void)
{
}

static void voldec(void)
{
}

static void key0(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 0);
}

static void key1(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 1);
}

static void key2(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 2);
}

static void key3(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 3);
}

static void key4(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 4);
}

static void key5(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 5);
}

static void key6(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 6);
}

static void key7(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 7);
}

static void key8(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 8);
}

static void key9(void)
{
  pmt->input(pmt, STO_MTDINPUT_CMD_KEY, 9);
}
